classdef dlnode < handle
% DLNODE  A class to represent a doubly-linked list node.
% Multiple dlnode objects may be linked together to create linked listes.
% Each node contains a piece of data and provides access to the next
% and previous nodes.
   properties
      Position
      V
   end
   properties(SetAccess = private)
      Next
      Prev
   end
    
   methods
      function node = dlnode(pos,v)
	      if nargin > 0
          node.Position = pos;
          node.V = v;
				else
					node.Position = [];
					node.V = [];
				end
      end
      
      function insertAfter(newNode, nodeBefore)
      % insertAfter  Inserts newNode after nodeBefore.
         disconnect(newNode);
         newNode.Next = nodeBefore.Next;
         newNode.Prev = nodeBefore;
         if ~isempty(nodeBefore.Next)
            nodeBefore.Next.Prev = newNode;
         end
         nodeBefore.Next = newNode;
      end
      
      function insertBefore(newNode, nodeAfter)
      % insertBefore  Inserts newNode before nodeAfter.
         disconnect(newNode);
         newNode.Next = nodeAfter;
         newNode.Prev = nodeAfter.Prev;
         if ~isempty(nodeAfter.Prev)
             nodeAfter.Prev.Next = newNode;
         end
         nodeAfter.Prev = newNode;
      end 

      function disconnect(node)
      % DISCONNECT  Removes a node from a linked list.  
      % The node can be reconnected or moved to a different list.
         Prev = node.Prev;
         Next = node.Next;
         if ~isempty(Prev)
             Prev.Next = Next;
         end
         if ~isempty(Next)
             Next.Prev = Prev;
         end
         node.Next = [];
         node.Prev = [];
      end
      
      function delete(node)
      % DELETE  Deletes a dlnode from a linked list.
         disconnect(node);
      end        

			function value = distance(node,aNode)
	      if isempty(aNode) || isempty(node)
	        fprintf(1,'distance, empty node\n');
					value = [];
					return;
	      end
				value = norm(aNode.Position - node.Position);
			end

			function plot(node,symbol,faceColor)
				if size(node.V,2) == 1
					plot(node.Position(1),node.Position(2),symbol,'MarkerFaceColor',faceColor);
				else
					%plot(node.Position(2),node.Position(1),symbol,'MarkerFaceColor',faceColor);
					h1=quiver(node.Position(2),node.Position(1),node.V(1)*2000,node.V(2)*2000,0);
					set(h1, 'Color',faceColor);
				end
			end

      function disp(node)
      % DISP  Displays a link node.
         %disp('Doubly-linked list node with data:')
				 if ~isempty(node.Position)
	         if size(node.V,2) == 1
						 fprintf(1,'\t(%f,%f) %f\n',node.Position(1), node.Position(2),node.V);
					 else
						 fprintf(1,'\t(%f,%f) %f %f %f\n',node.Position(1), node.Position(2),node.V(1),node.V(2),node.V(3));
					 end
				 else
					 fprintf(1,'\t() %f\n',node.V);
				 end
      end
   end % methods
end % classdef
